package com.haoyeteng.sxbd.modular.business.controller;

import com.baomidou.mybatisplus.plugins.Page;
import com.haoyeteng.sxbd.core.base.controller.BaseController;
import com.haoyeteng.sxbd.core.base.model.ResponseResult;
import com.haoyeteng.sxbd.core.common.annotion.Permission;
import com.haoyeteng.sxbd.core.common.constant.factory.PageFactory;
import com.haoyeteng.sxbd.core.log.LogObjectHolder;
import com.haoyeteng.sxbd.modular.business.model.Account;
import com.haoyeteng.sxbd.modular.business.model.EleFence;
import com.haoyeteng.sxbd.modular.business.service.IAccountService;
import com.haoyeteng.sxbd.modular.business.service.IEleFenceService;
import net.sf.json.JSONArray;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 电子围栏表控制器
 *
 * @author haoyeteng
 * @Date 2019-07-06 23:51:06
 */
@Controller
@RequestMapping("/eleFence")
public class EleFenceController extends BaseController {

    private String PREFIX = "/business/eleFence/";

    @Autowired
    private IEleFenceService eleFenceService;

    @Autowired
    private IAccountService accountService;

    /**
     * 跳转到电子围栏表首页
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "eleFence.html";
    }

    /**
     * 跳转到添加电子围栏表
     */
    @RequestMapping("/eleFence_add")
    public String eleFenceAdd() {
        return PREFIX + "eleFence_add.html";
    }

    /**
     * 跳转到修改电子围栏表
     */
    @RequestMapping("/eleFence_update/{eleFenceId}")
    public String eleFenceUpdate(@PathVariable Integer eleFenceId, Model model) {
        EleFence eleFence = eleFenceService.selectAndDtlById(eleFenceId);
        model.addAttribute("item",eleFence);
        model.addAttribute("details", JSONArray.fromObject(eleFence.getDetails()));
        LogObjectHolder.me().set(eleFence);
        return PREFIX + "eleFence_edit.html";
    }

    /**
     * 跳转到查看电子围栏表
     */
    @RequestMapping("/eleFence_detail/{eleFenceId}")
    public String eleFenceDetail(@PathVariable Integer eleFenceId, Model model) {
        EleFence eleFence = eleFenceService.selectAndDtlById(eleFenceId);
        model.addAttribute("item",eleFence);
        model.addAttribute("details", JSONArray.fromObject(eleFence.getDetails()));
        LogObjectHolder.me().set(eleFence);
        return PREFIX + "eleFence_detail.html";
    }

    /**
     * 跳转到查看 电子围栏查看账号
     */
    @RequestMapping("/eleFence_account/{eleFenceId}")
    public String eleFenceAccounts(@PathVariable Integer eleFenceId, Model model) {
        EleFence eleFence = eleFenceService.selectById(eleFenceId);
        model.addAttribute("eleFence", eleFence);
        return PREFIX + "eleFence_account.html";
    }

    /**
     * 电子围栏查看 账号列表
     */
    @RequestMapping(value = "/account/list")
    @ResponseBody
    public ResponseResult<Object> accountList(Integer eleFenceId) {
        Page<Account> page = new PageFactory<Account>().defaultPage();
        Map<String, Object> params = new HashMap<>();
        params.put("eleFenceId", eleFenceId);
        List<Map<String, Object>> accounts = accountService.selectAccounts(page, params);
        ResponseResult<Object> result = new ResponseResult<Object>();
        result.setData(accounts);
 		result.setCount(page.getTotal());
     	result.setCode(SUCCESS_TIP.getCode());
     	result.setMsg(SUCCESS_TIP.getMessage());
 		return result;
    }

    /**
     * 获取电子围栏表列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    @Permission
    public ResponseResult<Object> list(String fenceName,String startDate,String endDate) {
        Page<EleFence> page = new PageFactory<EleFence>().defaultPage();
        Map<String, Object> params = new HashMap<>();
        params.put("fenceName", fenceName);
        params.put("startDate", startDate);
        params.put("endDate", endDate);
        List<Map<String, Object>> eleFences = eleFenceService.selectEleFences(page, params);
        ResponseResult<Object> result = new ResponseResult<Object>();
        result.setData(eleFences);
 		result.setCount(page.getTotal());
     	result.setCode(SUCCESS_TIP.getCode());
     	result.setMsg(SUCCESS_TIP.getMessage());
 		return result;
    }

    /**
     * 新增电子围栏及明细坐标
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    @Permission
    public Object add(EleFence eleFence) {
        eleFenceService.insertAndDtl(eleFence);
        return SUCCESS_TIP;
    }

    /**
     * 删除电子围栏表
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    @Permission
    public Object delete(@RequestParam Integer eleFenceId) {
        eleFenceService.deleteById(eleFenceId);
        return SUCCESS_TIP;
    }

    /**
     * 修改电子围栏及明细坐标
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    @Permission
    public Object update(EleFence eleFence) {
        eleFenceService.updateAndDtl(eleFence);
        return SUCCESS_TIP;
    }

    /**
     * 电子围栏表详情
     */
    @RequestMapping(value = "/detail/{eleFenceId}")
    @ResponseBody
    public Object detail(@PathVariable("eleFenceId") Integer eleFenceId) {
        return eleFenceService.selectById(eleFenceId);
    }
}
